package com.smap.vector;

import java.util.ArrayList;
import java.util.List;

import com.smap.core.Bbox;
import com.smap.core.XY;

public abstract class Geometry {

	public List<XY> coordinates = new ArrayList<XY>();
	public Bbox envelope = new Bbox();

	/**
	 * use envelop to test
	 *
	 * @param point
	 * @return
	 */
	public boolean contains(XY point) {
		if (this.envelope == null) {
			return false;
		}
		return this.envelope.contains(point);
	}

	public void move(XY offset) {
		for (XY coord : coordinates) {
			coord.move(offset);
		}
		pack();
	}

	void clear() {
		envelope.clear();
		coordinates.clear();
	}

	public Geometry add(XY xy) {
		coordinates.add(xy);
		return this;
	}

	public Geometry add(double x, double y) {
		this.add(new XY(x, y));
		return this;
	}

	/**
	 * build envelope
	 */
	public Geometry pack() {
		if ((envelope.minX == null) && (coordinates.size() > 0)) {
			for (XY xy : coordinates) {
				envelope.expand(xy);
			}
		}
		return this;
	}

	List<LineSegment> getLineSegments() {
		if (coordinates.size() < 2)
			return null;
		List<LineSegment> lines = new ArrayList<LineSegment>();
		XY last = null;
		for (XY p : coordinates) {
			if (last != null) {
				lines.add(new LineSegment(last, p));
			}
			last = p;
		}
		return lines;
	}

	@Override
	public String toString() {
		return "Geometry [" + coordinates + "]";
	}

	public XY getCentroid(){
		return envelope.getCentroid();
	}

}
